//
//  MobilephoneChargeConfirmViewController.m
//  EasyToo
//
//  Created by YuanDee on 13-8-24.
//
//

#import "MobilephoneChargeConfirmViewController.h"
#import "NetworkAlertView.h"

@interface MobilephoneChargeConfirmViewController ()

@end

@implementation MobilephoneChargeConfirmViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)initScrollerView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    mobiledata = [MobileModel sharedata];
    [self initScrollerView];
    UIImage *image = [UIImage imageNamed:@"game_card"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    [imageView setBackgroundColor:[UIColor clearColor]];
     [self.view setBackgroundColor:BackgroundColor];
    [_bgScrollView addSubview:imageView];

	// Do any additional setup after loading the view.
    UILabel *label_message = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 150, 30)];
    label_message.text = @"确认信息";
    label_message.textColor = [UIColor redColor];
    label_message.font = [UIFont systemFontOfSize:20];
    label_message.backgroundColor = [UIColor clearColor];
    [label_message setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_message];
    
//    while (!mobiledata.isDataEnable) {
//        sleep(1);
//    }
    UILabel *label_phone = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 250, 30)];
    [label_phone setText:[NSString stringWithFormat:@"手机号码 : %@",mobiledata.phoneNum]];
    [label_phone setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_phone];
    
    
    UILabel *label_area = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 250, 30)];
    [label_area setText:[NSString stringWithFormat:@"所属区域 : %@",mobiledata.area]];
    [label_area setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_area];
    
    UILabel *label_carrier = [[UILabel alloc] initWithFrame:CGRectMake(50, 180, 250, 30)];
    [label_carrier setText:[NSString stringWithFormat:@"运  营  商 : %@",mobiledata.cardType]];
    [label_carrier setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_carrier];
    
    UILabel *label_pay = [[UILabel alloc] initWithFrame:CGRectMake(50, 230, 250, 30)];
    [label_pay setText:[NSString stringWithFormat:@"支付金额 : %@",mobiledata.price]];
    [label_pay setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_pay];
    
    UILabel *label_pay_type = [[UILabel alloc] initWithFrame:CGRectMake(50, 290, 250, 30)];
    [label_pay_type setText:[NSString stringWithFormat:@"支付方式 : %@",mobiledata.payType?@"支付宝":@"银联"]];
    [label_pay_type setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_pay_type];

    UILabel *label_sum = [[UILabel alloc] initWithFrame:CGRectMake(200, 340, 250, 30)];
    [label_sum setText:[NSString stringWithFormat:@"合计 : %@元",mobiledata.price]];
    label_sum.textColor = [UIColor redColor];
    label_sum.font = [UIFont systemFontOfSize:20];
    label_sum.backgroundColor = [UIColor clearColor];
    
    [_bgScrollView addSubview:label_sum];
    
    UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 380, 240, 40)];
    [confirmButton setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmPay:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton setTitle:@"确认支付" forState:UIControlStateNormal];
    
    [_bgScrollView addSubview:confirmButton];
}

- (void)confirmPay:(id)sender
{
//    UIAlertView *av= [[UIAlertView alloc] initWithTitle:@"提示" message:@"未登陆,请先登陆或注册" delegate:self  cancelButtonTitle:@"注册" otherButtonTitles:@"登陆", nil];
//    [av show];
//    [av release];
    //NSLog(@"UIBarButtonItem clicked");
    [self chargePhone];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)chargePhone
{
    mobiledata =[MobileModel sharedata];
    [PhoneManager shareManager].delegate = self;
    //弄个单位是分的价格传给txamt
    NSInteger price = [mobiledata.price integerValue];
    price = price * 100;
    NSString* m_price = [NSString stringWithFormat:@"%d", price];
//    mobiledata.price = [NSString stringWithFormat:@"%d", price];
    [[PhoneManager shareManager] chargePhone:mobiledata.phoneNum cardId:mobiledata.cardId cardPrice:m_price];
}

- (void)onChargePhoneFinish:(BOOL)isSuccess
                         tn:(NSString*)tn
{
    [[NetworkAlertView shareView] delView];
    NSLog(@"tn:%@", tn);
    [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:@"00" viewController:self delegate:self];
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}


@end
